from crm_app import models
from stark.service.stark import StarkConfig, Option
from django import forms
from crm_app.permission.base import BASERbacPermission


class ConsultRecordConfig(BASERbacPermission, StarkConfig):
    """所有客户的跟进记录"""
    list_display = ["customer", "consultant", "note"]

    def get_queryset(self):
        # 显示所有跟进记录 or 显示某一个客户的跟进记录
        cid = self.request.GET.get("cid")
        if cid:
            return models.ConsultRecord.objects.filter(customer_id=cid)
        return models.ConsultRecord.objects


class PrivateModelForm(forms.ModelForm):
    class Meta:
        model = models.ConsultRecord
        exclude = ['customer', 'consultant']


class PrivateConsultRecordConfig(BASERbacPermission, StarkConfig):
    """私户客户的跟进记录"""
    list_display = ["customer", "consultant", "note"]
    model_form_class = PrivateModelForm

    def get_queryset(self):
        # 私户的跟进记录
        current_user_id = 1  # 以后要改成当前登录用户的id
        cid = self.request.GET.get("cid")
        if cid:
            return models.ConsultRecord.objects.filter(customer_id=cid, customer__consultant_id=current_user_id)
        return models.ConsultRecord.objects.filter(customer__consultant_id=current_user_id)

    def save(self, form, modify=False):
        if not modify:
            params = self.request.GET.get("_filter")
            cid, num = params.split("=")
            form.instance.customer = models.Customer.objects.get(id=num)

            current_user_id = 1  # 以后要改成当前登录用户的id
            form.instance.consultant = models.UserInfo.objects.get(id=current_user_id)

        form.save()